package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.PayOrderInfoService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.PayOrderInfo;
import com.iot08.common.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/2/11 9:23
 **/
@Slf4j
@Controller
@RequestMapping("payOrderInfo")
public class PayOrderInfoController extends BaseController {

    @Resource
    private PayOrderInfoService payOrderInfoService;

    @RequestMapping("list")
    public ModelAndView list(PageVO page,
                             @RequestParam(value = "startTime", required = false) String startTime,
                             @RequestParam(value = "endTime", required = false) String endTime,
                             @RequestParam(value = "transactionId",required = false) String transactionId,
                             @RequestParam(value = "phoneNum",required = false) String phoneNum,
                             @RequestParam(value = "payType", required = false) String payType,
                             @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                             @RequestParam(value = "orderType", required = false) Integer orderType,
                             @RequestParam(value = "productType", required = false) Integer productType) {
        ModelAndView mv = new ModelAndView();
        String id = page.getSearchValue();
        Page<PayOrderInfo> pages = payOrderInfoService.list(page, startTime,endTime,transactionId,phoneNum,payType,orderStatus,orderType,productType);
        mv.addObject("payOrderInfos", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("payType",payType);
        mv.addObject("orderStatus",orderStatus);
        mv.addObject("orderType",orderType);
        mv.addObject("productType",productType);
        mv.addObject("transactionId",transactionId);
        mv.addObject("phoneNum",phoneNum);
        mv.addObject("startTime",startTime);
        mv.addObject("endTime",endTime);
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        if(ObjectUtils.isEmpty(orderStatus) || Integer.valueOf(1).equals(orderStatus)) {
            mv.addObject("payTotal",payOrderInfoService.queryPayTotalByParam(id,startTime,endTime,transactionId,payType,orderType,productType));
        }
        mv.setViewName("payorder/payorderinfo_list");
        return mv;
    }

    
    /**
     * @description 按查询条件导出数据
     * @author cwj
     * @param [startTime, endTime, transactionId, payType, orderStatus]
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/4/18 17:23
     **/
    @RequestMapping("import")
    public void importData(@RequestParam(value = "orderId", required = false) String orderId,
                                       @RequestParam(value = "startTime", required = false) String startTime,
                                       @RequestParam(value = "endTime", required = false) String endTime,
                                       @RequestParam(value = "transactionId",required = false) String transactionId,
                                       @RequestParam(value = "payType", required = false) String payType,
                                       @RequestParam(value = "orderStatus", required = false) Integer orderStatus,
                                       @RequestParam(value = "orderType", required = false) Integer orderType,
                                       @RequestParam(value = "productType", required = false) Integer productType,
                                       HttpServletResponse response)  {
         payOrderInfoService.importData(orderId,startTime,endTime,transactionId,payType,orderStatus,orderType,productType,response);
    }


    /**
     * @description 查询支付成功的总金额
     * @author cwj
     * @param []
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/5/14 9:45
     **/
    @ResponseBody
    @RequestMapping(value = "queryPayTotal", method = RequestMethod.POST)
    public Response<Object> queryPayTotal(){
        return payOrderInfoService.queryPayTotal();
    }
}
